package com.idconfirmsystem.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.idconfirmsystem.R;
import com.idconfirmsystem.activity.TaskDetailActivity;
import com.idconfirmsystem.model.Task;

public class TaskAdapter extends BaseAdapter {

	private List<Task> tdTasks = new ArrayList<Task>();
	private Context context;
	ListView listView;
	Task task;

	public TaskAdapter(Context context, ListView listView) {
		this.context = context;
		this.listView = listView;
	}

	public TaskAdapter(Context context, ListView listView, List<Task> list) {
		this.context = context;
		this.listView = listView;
		this.tdTasks = list;
	}

	public void resetData(List<Task> list) {
		this.tdTasks.clear();
		this.tdTasks.addAll(list);
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return tdTasks.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return tdTasks.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_task, null);
			viewHolder = new ViewHolder();
			viewHolder.tv1 = (TextView) convertView.findViewById(R.id.tv_taskcode);
			viewHolder.tv2 = (TextView) convertView.findViewById(R.id.tv_sendoutTime);
			viewHolder.tv3 = (TextView) convertView.findViewById(R.id.tv_chargor);
			viewHolder.tv4 = (TextView) convertView.findViewById(R.id.tv_driver);
			viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv_dotask);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		task = tdTasks.get(position);
		viewHolder.tv1.setText("任务编号："+task.getId());
//		if(task.getDeparture_time()!=null){
//			viewHolder.tv2.setText(task.getDeparture_time().toLocaleString());
//		}
		viewHolder.tv2.setText("守押员："+task.getEscort_name1()+","+task.getEscort_name2());
		viewHolder.tv3.setText("业务员："+ task.getClerk_name());
		viewHolder.tv4.setText("驾驶员："+ task.getPilot_name());
		viewHolder.iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, TaskDetailActivity.class);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	public class ViewHolder {
		public TextView tv1;
		public TextView tv2;
		public TextView tv3;
		public TextView tv4;
		public ImageView iv;
	}
}
